package com.superb.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.superb.common.core.annotation.SuperbDataScope;
import com.superb.common.core.annotation.SuperbRestMapping;
import com.superb.common.core.model.Result;
import com.superb.common.database.vo.BatchId;
import com.superb.common.database.vo.PageParams;
import com.superb.common.redis.key.KeyType;
import com.superb.common.redis.key.RedisKey;
import com.superb.common.redis.utils.RedisUtils;
import com.superb.common.utils.StringUtils;
import com.superb.system.api.entity.SystemTenant;
import com.superb.system.service.SystemTenantService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * 系统租户管理表;(system_tenant)表控制层
 * @Author: ajie
 * @CreateTime: 2024-5-11
 */
@SuperbDataScope
@SuperbRestMapping("/systemTenant")
@Tag(name = "系统租户管理表接口")
@ApiSupport(author = "阿杰：ajie20999@163.com", order = 2)
public class SystemTenantController {

    @Resource
    private SystemTenantService systemTenantService;

    @PostMapping("pageQuery")
    @ApiOperationSupport(order = 1)
    @SaCheckPermission("system:tenant:select")
    @Operation(summary = "分页查询", description = "权限: system:tenant:select<br>")
    public Result<Page<SystemTenant>> pageQuery(@RequestBody PageParams<SystemTenant> pageParams) {
        SystemTenant params = pageParams.getParams();
        LambdaQueryWrapper<SystemTenant> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotBlank(params.getTenantKey()), SystemTenant::getTenantKey, params.getTenantKey())
                .like(StringUtils.isNotBlank(params.getName()), SystemTenant::getName, params.getName())
                .like(StringUtils.isNotBlank(params.getLegalPerson()), SystemTenant::getLegalPerson, params.getLegalPerson())
                .like(StringUtils.isNotBlank(params.getPhone()), SystemTenant::getPhone, params.getPhone());
        Page<SystemTenant> result = systemTenantService.page(pageParams.getPage(), lqw);
        return Result.success(result);
    }

    @PostMapping("insert")
    @ApiOperationSupport(order = 2)
    @SaCheckPermission("system:tenant:insert")
    @Operation(summary = "新增信息", description = "权限:system:tenant:insert<br>")
    public Result<Boolean> insert(@RequestBody @Validated SystemTenant systemTenant) {
        if (systemTenantService.save(systemTenant)) {
            return Result.success();
        }
        return Result.error();
    }

    @PostMapping("update")
    @ApiOperationSupport(order = 3)
    @SaCheckPermission("system:tenant:update")
    @Operation(summary = "修改信息", description = "权限: system:tenant:update<br>")
    public Result<Boolean> update(@RequestBody @Validated SystemTenant systemTenant) {
        if (systemTenantService.updateById(systemTenant)) {
            RedisKey key1 = new RedisKey(KeyType.PER, "tenantInfo");
            RedisKey key2 = new RedisKey(KeyType.PER, "info");
            RedisUtils.build().del(key1, key2);
            return Result.success();
        }
        return Result.error();
    }

    @GetMapping("remove/{id}")
    @ApiOperationSupport(order = 4)
    @SaCheckPermission("system:tenant:update")
    @Operation(summary = "根据id删除", description = "权限: system:tenant:update<br><font style='color: #1890FF'>假删除，会进入回收站</font>", parameters = {
            @Parameter(name = "id", description = "业务主键", required = true, in = ParameterIn.PATH)
    })
    public Result<Boolean> removeById(@PathVariable String id) {
        if (systemTenantService.update(Wrappers.<SystemTenant>lambdaUpdate().set(SystemTenant::getDel, 1).eq(SystemTenant::getId, id))) {
            RedisKey key1 = new RedisKey(KeyType.PER, "tenantInfo");
            RedisKey key2 = new RedisKey(KeyType.PER, "info");
            RedisUtils.build().del(key1, key2);
            return Result.success();
        }
        return Result.error();
    }

    @GetMapping("info/{id}")
    @ApiOperationSupport(order = 4)
    @Operation(summary = "根据id获取信息", parameters = {
            @Parameter(name = "id", description = "业务主键", required = true, in = ParameterIn.PATH)
    })
    public Result<SystemTenant> getInfo(@PathVariable String id) {
        SystemTenant tenant = systemTenantService.getById(id);
        return Result.success(tenant);
    }

    @GetMapping("delQuery")
    @ApiOperationSupport(order = 5)
    @SaCheckPermission({"system:tenant:select", "system:tenant:delete"})
    @Operation(summary = "回收站", description = "权限: <br>同时拥有system:tenant:select、system:tenant:delete")
    public Result<List<SystemTenant>> delQuery() {
        List<SystemTenant> result = systemTenantService.delPage();
        return Result.success(result);
    }

    @PostMapping("recovery")
    @ApiOperationSupport(order = 6)
    @SaCheckPermission({"system:tenant:update", "system:tenant:delete"})
    @Operation(summary = "根据id恢复删除的数据", description = "权限: <br>同时拥有system:tenant:update、system:tenant:delete")
    public Result<Boolean> recovery(@RequestBody @Validated BatchId batchId) {
        if (systemTenantService.recoveryByBatchId(batchId.getId())) {
            return Result.success();
        }
        return Result.error();
    }

    @PostMapping("delete")
    @ApiOperationSupport(order = 7)
    @SaCheckPermission("system:tenant:delete")
    @Operation(summary = "根据id彻底删除", description = "权限: system:tenant:delete<br><font style='color: red'>真删除，数据无法恢复</font>")
    public Result<Boolean> delete(@RequestBody @Validated BatchId batchId) {
        if (systemTenantService.deleteByBatchId(batchId.getId())) {
            return Result.success();
        }
        return Result.error();
    }

}